package br.com.basili.mb;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import br.com.basili.dao.ProtocoloDAO;
import br.com.basili.entity.Protocol;
import br.com.basili.entity.Researcher;

@ManagedBean
@ViewScoped
public class RelatorioBean {
	
	private Protocol protocol;
	private List<Protocol> listaProtocolo;
	ProtocoloDAO dao;
	private static final String PESQUISADORES = "Pesq";
	private String pesquisadores;
	
	public RelatorioBean() {
		 protocol = new Protocol();
		 listaProtocolo = new ArrayList<Protocol>();
		 dao = new ProtocoloDAO();
		 
	}
	
	public List<Protocol> getListProtocol() throws Exception{
		return dao.getListProtocol();
	}
	
    public void gerarRelatorio( Protocol p ) {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        ServletContext context = (ServletContext) externalContext.getContext();
        String arquivo = context.getRealPath("WEB-INF/relatorios/basi.jasper");
        Map<Object, Object> parametros = new HashMap<Object, Object>();
        
        listaProtocolo.add(p);
        for(Researcher r : p.getIdentification().getResearchers())
        		pesquisadores += r.getName() + " (" + r.getEmail() + "), ";
        
        pesquisadores = pesquisadores.substring(0, pesquisadores.length()-1);

        JRDataSource jrds = new JRBeanCollectionDataSource(listaProtocolo);
        parametros.put(PESQUISADORES, pesquisadores);
        
        gerarRelatorioWeb(jrds, parametros, arquivo);
        
        listaProtocolo.remove(getProtocol());
//        protocol = new Protocol();
    }
 
    private void gerarRelatorioWeb(JRDataSource jrds, Map<Object, Object> parametros, String arquivo) {
        ServletOutputStream servletOutputStream = null;
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
 
        try {
            servletOutputStream = response.getOutputStream();
            JasperRunManager.runReportToPdfStream(new FileInputStream(new File(arquivo)), response.getOutputStream(), parametros, jrds);
            response.setContentType("application/pdf");
            servletOutputStream.flush();
            servletOutputStream.close();
            context.renderResponse();
            context.responseComplete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

	public Protocol getProtocol() {
		return protocol;
	}

	public void setProtocol(Protocol protocol) {
		this.protocol = protocol;
	}

	public void setListaProtocolo(List<Protocol> listaProtocolo) {
		this.listaProtocolo = listaProtocolo;
	}

	public List<Protocol> getListaProtocolo() {
		return listaProtocolo;
	}
	
}